package com.xinshujia.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.xinshujia.dto.RoleDTO;
import com.xinshujia.entity.RoleEntity;
import com.xinshujia.response.RespResult;
import com.xinshujia.service.RoleService;
import com.xinshujia.utils.GsonUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-15 19:39       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 *     角色控制器
 */
@RestController
@RequestMapping("role")
public class RoleController {
    private final RoleService roleService;

    public RoleController(RoleService roleService)
    {
        this.roleService = roleService;
    }

    /**
     * 分页查询
     */
    @GetMapping("getRolesByPage")
    public RespResult getRolesByPage(@RequestParam("pageNum") Long pageNum,
                                     @RequestParam("pageSize") Long pageSize,
                                     @RequestParam("roleName") String roleName) {
        Page<RoleDTO> roles = roleService.getRoleByPage(pageNum, pageSize, roleName);
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(roles));
    }

    /**
     * 添加角色
     */
    @PostMapping("insertRole")
    public RespResult insertRole(@RequestBody RoleEntity roleEntity){
        roleEntity.setRoleName("ROLE_" + roleEntity.getRoleName());
        boolean flag = roleService.save(roleEntity);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

    /**
     * 删除
     */
    @GetMapping("deleteRoleById")
    public RespResult deleteById(@RequestParam("id") Long id){
        boolean flag = roleService.removeById(id);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

    /**
     * 修改
     */
    @PostMapping("updateRoleById")
    public RespResult updateRoleById(@RequestBody RoleEntity roleEntity){
        boolean flag = roleService.updateById(roleEntity);
        return flag ? RespResult.OK() : RespResult.Fail();
    }

    /**
     * 查找所有的角色
     */
    @GetMapping("getRoles")
    public RespResult getRoles(){
        List<RoleEntity> roles = roleService.getRoles();
        Gson gson = GsonUtil.googleGson();
        return RespResult.OK(gson.toJson(roles));
    }


    /**
     * 给角色分配资源
     */
    @GetMapping("setResourcesForRole")
    public RespResult setResourcesForRole(@RequestParam("roleId") Long roleId,
                                      @RequestParam("resourceIds") String resourceIds) {
        boolean flag = roleService.setResourcesForRole(roleId, resourceIds);
        if (flag) {
            return RespResult.OK();
        } else {
            return RespResult.Fail();
        }
    }


    /**
     * 给角色添加设备
     */
    @GetMapping("setDevicesForRole")
    public RespResult setDevicesForRole(@RequestParam("roleId") Long roleId,
                                          @RequestParam("deviceIds") String deviceIds) {
        boolean flag = roleService.setDevicesForRole(roleId, deviceIds);
        if (flag) {
            return RespResult.OK();
        } else {
            return RespResult.Fail();
        }
    }

}
